import 'package:flutter/material.dart';

class DoubleBackExitPage extends StatefulWidget {
  @override
  _DoubleBackExitPageState createState() => _DoubleBackExitPageState();
}

class _DoubleBackExitPageState extends State<DoubleBackExitPage> {
  DateTime? _lastPressedAt; //上次点击时间

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvoked: (bool didPop) async {
        if (!didPop) {
          final now = DateTime.now();
          if (_lastPressedAt == null ||
              now.difference(_lastPressedAt!) > Duration(seconds: 1)) {
            _lastPressedAt = now;
            ScaffoldMessenger.of(
              context,
            ).showSnackBar(SnackBar(content: Text('再次按下返回键退出')));
          } else {
            Navigator.of(context).pop();
          }
        }
      },
      child: Scaffold(
        appBar: AppBar(title: Text('Double Back Exit')),
        body: Center(child: Text('1秒内连续按两次返回键退出')),
      ),
    );
  }
}
